import struct


def format_send_message(data, need_tow_bytes=False):
    '''
    :param data:
    :param need_tow_bytes: 在生成协议时候，有的16进制需要2个字节为。如 "00 64"
    :return: 字节流
    '''
    if isinstance(data, str):
        data = int(data)

    if data is None:
        data = 0

    if isinstance(data, int):
        if data < 256 and not need_tow_bytes:
            return struct.pack(">B", data)
        else:
            return struct.pack(">H", data)
    elif isinstance(data, str):
        return struct.pack(">c", data)


def bytes2int(data: bytes):
    '''
    :param data: b"\xff\xff"
    :return: int : 65535
    '''
    return int.from_bytes(data, byteorder='big')